home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Information / WebSites / Wirenet / files / Dashboard2.lha / Dashboard2 / AmiTCP / bin / Connect < prev    next >
Text File  |  1998-03-27  |  10KB  |  312 lines

  1. /*
  2. $VER: Connect 2.0 (26.3.98)
  3. (c) Neil Bothwick
  4.  
  5. Connects your Amiga to the Internet, optionally
  6. fetching news or mail and logging off again
  7. */
  8.  
  9. address command 'StartNet'
  10.  
  11. /* ;;; Usage
  12.     Name
  13.         Connect
  14.  
  15.     Usage
  16.         Connect [MAIL|NEWS|AUTOMAIL|AUTONEWS|OFF|AUTOOFF]
  17.  
  18.     Options
  19.         None:       Connects to Internet
  20.         MAIL:       Connects to Internet and fetches mail
  21.         NEWS:       Connects to Internet and fetches news and mail
  22.         AUTOMAIL:   Connects to Internet, fetches mail and disconnects
  23.         AUTONEWS:   Connects to Internet, fetches news and mail and disconnects
  24.         OFF:        Disconnects from Internet
  25.         AUTOOFF:    Disconnects from Internet after waiting for news and mail to finish
  26.  
  27. */
  28. ;;;
  29. /* ;;;These are specific to your setup */
  30.  
  31. DevName    = 'DEVS:Networks/ppp.device'                             /* Your network device */
  32. DevUnit    = 0                                                      /* its unit number */
  33. Interface  = 'ppp'||DevUnit                                         /* Interface name */
  34. ;;;
  35. /* ;;;Don't change anything below here */
  36. BinPath    = 'Wirenet:bin/'                                         /* Paths to AmiTCP commands */
  37. ThorPath   = GetVar('Thor/ThorPath')
  38. Miami      = 'Miami:Miami'
  39. StartNet   = BinPath||'StartNet'
  40. ifconfig   = BinPath||'ifconfig'
  41. route      = BinPath||'route'
  42. online     = BinPath||'online'
  43. offline    = BinPath||'offline'
  44. resolve    = BinPath||'resolve'
  45. SendEvents = BinPath||'SendEvents'
  46. GetMail    = BinPath||'Fetch Mail'
  47. GetNews    = BinPath||'Fetch All'
  48. LogPath    = 'UUSpool:Info.log'
  49. SendProc   = ThorPath||'bin/SendTCP'                                /* CLI process names for news and mail */
  50. GetProc    = ThorPath||'bin/GetTCP'
  51. Unbatch    = BinPath||'ParseThor'
  52. cr         = '0a'x
  53.  
  54. options results
  55. address command
  56. arg opt
  57. ;;;
  58. /* ;;;Main routine */
  59. if pos(upper(getclip('TCPstack')),'ONYESTRUE1') > 0 then TCPstack = 1
  60. else TCPstack = 0
  61.  
  62. if getclip('IPAddress') = '0.0.0.0' then AcctType = 'Dynamic'
  63. else AcctType = 'Static'
  64.  
  65. select
  66.     when opt = '' then call Connect
  67.     when opt = 'OPEN' then call Connect
  68.     when opt = 'MAIL' then do
  69.         call Connect
  70.         call DoMail
  71.         end
  72.     when opt = 'NEWS' then do
  73.         call Connect
  74.         call DoNews
  75.         end
  76.     when opt = 'AUTOMAIL' then do
  77.         call Connect
  78.         call DoMail
  79.         'Wait 20'
  80.         call Auto
  81.         call Disconnect
  82.         end
  83.     when opt = 'AUTONEWS' then do
  84.         call Connect
  85.         call DoNews
  86.         'Wait 20'
  87.         call Auto
  88.         call Disconnect
  89.         end
  90.     when opt = 'OFF' then call Disconnect
  91.     when opt = 'AUTOOFF' then do
  92.         call Auto
  93.         call Disconnect
  94.         end
  95.     otherwise call BadArgs
  96.     end
  97. ;;;
  98. exit
  99.  
  100. /* Procedures */
  101.  
  102. /* ;;;Establish connection */
  103. Connect:
  104.     StartNet
  105.     if CheckOnline() > 0 then do
  106.         if rtezrequest('You are already online'cr'Send outstanding events?','_Yes|_No','Connect','rt_reqpos = reqpos_centerscr') = 1 then do
  107.             'CheckCmd >NIL:' SendProc
  108.             if RC = 0 then call rtezrequest('Already sending events',' OK ','Connect','rt_reqpos = reqpos_centerscr')
  109.             else 'run' SendEvents
  110.             end
  111.         exit
  112.         end
  113.  
  114.     if exists('Wirenet:UserScripts/PreConnect') then address command 'Wirenet:UserScripts/PreConnect'
  115.     if TCPstack = 0 then do
  116.         call MakeDialScript()
  117.         online Interface
  118.         if RC > 0 then call ConnectionFailed
  119.         ifconfig 'lo0 localhost'
  120.         ifconfig Interface '$ppp0IPLocal $ppp0IPRemote'
  121.         if AcctType = 'Dynamic' then do                             /* Set up for dynamic account */
  122.             address 'AMITCP' 'ADD HOST' getvar('ppp0IPLocal') getclip('HostName')
  123.             route 'add $ppp0IPLocal localhost'
  124.             end
  125.         route 'add default $ppp0IPRemote'
  126.         call WriteLog('Connection made to' GetVar('ppp0IPRemote'))
  127.         end
  128.     else do
  129.         address 'MIAMI.1' 'ONLINE'
  130.         if CheckOnline() = 0 then call ConnectionFailed
  131.         call WriteLog('Connection made')
  132.         end
  133.     if exists('Wirenet:UserScripts/PostConnect') then address command 'Wirenet:UserScripts/PostConnect'
  134.     options failat 21
  135.     'delete >NIL: ENV:sana2/ppp.#?'
  136.     'delete >NIL: ENV:sana2/DialScript'
  137.     options failat 10
  138.     'run' SendEvents
  139.     'Run >NIL: GetStatus'
  140.     return
  141. ;;;
  142. /* ;;;Close connection */
  143. Disconnect:
  144.     if exists('Wirenet:UserScripts/PreDisconnect') then address command 'Wirenet:UserScripts/PreDisconnect'
  145.     if TCPstack = 0 then do
  146.         ifconfig interface 'down'
  147.         offline Interface
  148.         route 'delete >NIL: $ppp0IPLocal'
  149.         route 'delete >NIL: default'
  150.         end
  151.     else do
  152.         address 'MIAMI.1' 'OFFLINE'
  153.         end
  154.     call WriteLog('Connection closed')
  155.     Unbatch
  156.     if exists('Wirenet:UserScripts/PostDisconnect') then address command 'Wirenet:UserScripts/PostDisconnect'
  157.     return
  158. ;;;
  159. /* ;;;Start mail download */
  160. DoMail:
  161.     'run >NIL:' GetMail
  162.     call WriteLog('Mail download started')
  163.     return
  164. ;;;
  165. /* ;;;Start mail and news download */
  166. DoNews:
  167.     'Run >NIL:' GetNews
  168.     call WriteLog('News download started')
  169.     return
  170. ;;;
  171. /* ;;;Wait for mail and news processes to stop */
  172. Auto:
  173.     say 'Waiting for news/mail to finish'
  174.     call WaitToEnd(SendProc GetProc 'GetTCP' 'SendTCP' 'ncftp' 'tcpdl' 'httpj' 'GrabHTTP')
  175.     'Wait 2'
  176.     call WaitToEnd(SendProc GetProc 'GetTCP' 'SendTCP' 'ncftp' 'tcpdl' 'httpj' 'GrabHTTP')
  177.     return
  178. ;;;
  179. /* ;;;Give usage information */
  180. BadArgs:
  181.     say
  182.     say 'Usage: Connect [MAIL|NEWS|AUTOMAIL|AUTONEWS|OFF|AUTOOFF]'
  183.     say
  184.     say '       None:       Connects to Internet'
  185.     say '       MAIL:       Connects to Internet and fetches mail'
  186.     say '       NEWS:       Connects to Internet and fetches news and mail'
  187.     say '       AUTOMAIL    Connects to Internet, fetches mail and disconnects'
  188.     say '       AUTONEWS:   Connects to Internet, fetches news and mail and disconnects'
  189.     say '       OFF:        Disconnects from Internet'
  190.     say '       AUTOOFF:    Disconnects from Internet after waiting for news and mail to finish'
  191.     say
  192.     exit
  193.  ;;;
  194. /* ;;;Write line in logfile */
  195. WriteLog: procedure expose LogPath
  196.     parse arg msg
  197.     LogEntry = left(date('W'),3) date() time() msg
  198.     say LogEntry
  199.     if ~open(log,LogPath,'A') then do
  200.         if ~open(log,LogPath,'W') then return
  201.         end
  202.     call writeln(log,LogEntry)
  203.     call close(log)
  204.     return
  205. ;;;
  206. /* ;;;Wait for the specified process to finish */
  207. WaitToEnd: procedure
  208.     parse arg Processes
  209.  
  210.     procs = words(Processes)
  211.     do i = 1 to procs
  212.         parse var Processes Process.i Processes
  213.         end
  214.  
  215.     do until Busy = FALSE
  216.         Busy = FALSE
  217.         do i = 1 to procs
  218.             'CheckCmd >NIL:' Process.i
  219.             if RC = 0 then do
  220.                 Busy = TRUE
  221.                 'Wait 3'
  222.                 end
  223.             end
  224.         end
  225.     return
  226. ;;;
  227. /* ;;;Report connection failure */
  228. ConnectionFailed:
  229.     call WriteLog('Connection attempt failed')
  230.     ExitMsg('Your internet connection has failed')
  231.     return
  232. ;;;
  233. /* ;;;Exit with message */
  234. ExitMsg:
  235.     parse arg ErrMsg
  236.     say(ErrMsg)
  237.     exit
  238.     return
  239. ;;;
  240. /* ;;;Create dialscript and ppp0.config */
  241. MakeDialScript:
  242.     /* Create DialScript */
  243.     if ~open('ds','ENV:sana2/DialScript','W') then call ExitMsg('Failed to create dialscript')
  244.     call writeln('ds','# Dialer Script (change to echo off if you do not want echoing)')
  245.     call writeln('ds','echo on')
  246.     call writeln('ds','abort "NO DIALTONE"')
  247.     call writeln('ds','redial "BUSY" "NO CARRIER" "NO ANSWER"')
  248.     call writeln('ds','redialdelay 300')
  249.     call writeln('ds','timeout 500')
  250.     call writeln('ds','# Initialise modem...')
  251.     call writeln('ds','send "'||getclip('ModemInit')||'"')
  252.     call writeln('ds','wait "OK"')
  253.     call writeln('ds','delay 15')
  254.     call writeln('ds','# Send dial command...')
  255.     call writeln('ds','send "'||getclip('ModemDial')||getclip('PopNum')||'"')
  256.     call writeln('ds','# ... and wait for connection.')
  257.     call writeln('ds','timeout 1600')
  258.     call writeln('ds','wait "CONNECT"')
  259.     if pos(upper(getclip('UsePAP')),'ONYESTRUE1') > 0  then do
  260.         papcfg = 'ENV:sana2/ppp.'time(s)
  261.         if ~open('pc',papcfg,'W') then call ExitMsg('Failed to create pap config')
  262.         call writeln('pc',getclip('NodeName') getclip('Password'))
  263.         call close('pc')
  264.         end
  265.     else do
  266.         call writeln('ds','send ""')
  267.         call writeln('ds','# At login: prompt send nodename')
  268.         call writeln('ds','wait "ogin:"')
  269.         call writeln('ds','send "'||getclip('NodeName')||'"')
  270.         call writeln('ds','# At Password: prompt send password')
  271.         call writeln('ds','wait "assword:"')
  272.         call writeln('ds','echo off')
  273.         call writeln('ds','send "'||getclip('Password')||'"')
  274.         call writeln('ds','echo on')
  275.         call writeln('ds','# Wait for Service: prompt and then send PPP')
  276.         call writeln('ds','wait "ervice:"')
  277.         call writeln('ds','send "PPP"')
  278.         call writeln('ds','# Wait for start string')
  279.         call writeln('ds','wait "MTU"')
  280.         call writeln('ds','send " "')
  281.         call writeln('ds','# Connected!')
  282.         end
  283.     call close('ds')
  284.  
  285.     /* Create PPP config file */
  286.     if ~open(pppcfg,'ENV:sana2/ppp0.config','w') then ExitMsg('Failed to create ppp configuration')
  287.     call writech(pppcfg,getclip('ModemDev')' ')
  288.     call writech(pppcfg,getclip('ModemSpeed')' ')
  289.     call writech(pppcfg,getclip('IPAddress')' ')
  290.     call writech(pppcfg,'DialScript ENV:sana2/DialScript ')
  291.     call writech(pppcfg,'MTU='getclip('MTU')' ')
  292.     call writech(pppcfg,getclip('PPPoptions'))
  293.     if pos(upper(getclip('UsePAP')),'ONYESTRUE1') > 0 then call writech(pppcfg,' PAP='papcfg','getclip('NodeName'))
  294.     if getclip('DialWindow') > '' then call writech(pppcfg,' DIALWINDOW ' getclip('DialWindow'))
  295.     if getclip('Gateway') > '' then call writech(pppcfg,' REMOTEIP' getclip('Gateway'))
  296.     call close(pppcfg)
  297.  
  298.     return
  299. ;;;
  300. /* ;;; Check if online */
  301. CheckOnline:
  302.     if TCPstack then do
  303.         address 'MIAMI.1' 'ISONLINE'
  304.         return RC
  305.         end
  306.     else
  307.         'ifcheck >NIL:' Interface
  308.         return (RC=0)
  309.         end
  310. ;;;
  311.  
  312.